package com.woniuxy.woniuhis.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Address;
import com.woniuxy.woniuhis.exception.AddressServiceException;
import com.woniuxy.woniuhis.mapper.AddressMapper;
import com.woniuxy.woniuhis.service.AddressService;
import com.woniuxy.woniuhis.util.UUIDUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void addAddress(Address address) {
        try {
            if (address.getAddressId() == null) {
                address.setAddressId(UUIDUtil.uuid32());
            }
            addressMapper.insertAddress(address);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressServiceException("服务维护中");
        }
    }

    @Override
    public void delAddress(String addressId) {
        try {
            if (!StringUtils.isEmpty(addressId)) {
                addressMapper.deleteAddressById(addressId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressServiceException("服务维护中");
        }
    }

    @Override
    public void modifyAddress(Address address) {
        try {
            if (address != null) {
                addressMapper.updateAddress(address);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressServiceException("服务维护中");
        }
    }

    @Override
    public Address findAddressById(String addressId) {
        try {
            if (!StringUtils.isEmpty(addressId)) {
                return addressMapper.selectAddressById(addressId);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressServiceException("服务维护中");
        }
    }

    @Override
    public PageInfo<Address> findAddressListByCondition(Address condition, Integer pageNum, Integer pageSize) {
        try {
            if (pageNum == null) {
                pageNum = 0;
            }
            if (pageSize == null || pageSize <= 0) {
                //默认返回五条数据
                pageSize = 5;
            }
            return new PageInfo<Address>(addressMapper.selectAddressListByCondition(condition, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressServiceException("服务维护中");
        }
    }
}
